<?php
error_reporting(E_ALL); 
require('xfb/xfbpay.php');
class demo{
     public function __construct() {
		 $this->shang_hu_hao = '000201505130444481';//商户号
		 $this->mishi='9aa5194b70aadf1da90745c39d5b89fd';//密匙
		 $sn='gzy'.date('YmdHis',time()).rand();
		 $this->pay_sn=$sn;//订单号
		 $this->pay_amount_online = '0.01';//金额
		 $this->xfbpay = new Xfbpay();
    }
	//微信 h5支付  jsapi预下单
	public function jsapi_pre_order()
	{
		//微信
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['totalfee']=$this->pay_amount_online;//消费总金额
		$xfbarr['body']='祥付宝支付';//
		$xfbarr['subject']='订单标题';//订单标题
		$xfbarr['paymethod']='wx';//支付方式
		$xfbarr['funname']='prepay';//
		$xfbarr['tradetype']='JSAPI';//微信内线上支付
		$xfbarr['subopenid']='openid 需要从微信获取';//子商户的用户openid
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,1);
		return $xfb_erweima;
	}
    //获取祥付宝微信二维码
    public function weixin()
    {
		//微信
		$xfbarr['orderid']=$this->pay_sn;//订单号
		$xfbarr['merid']=$this->shang_hu_hao;//商户号
		$xfbarr['totalfee']=$this->pay_amount_online;//消费总金额
		$xfbarr['body']='祥付宝支付';//
		$xfbarr['subject']='订单标题';//订单标题
		$xfbarr['paymethod']='wx';//支付方式
		$xfbarr['funname']='prepay';//
		$xfb_erweima = $this->xfbpay->send_xfb($xfbarr,$this->mishi,1);
		return $xfb_erweima;
		/*$flag = $xfb_erweima['flag'][0]['#text'];
		//获取二维码成功
		if($flag==00){
			$wxarr['codeurl'] = $xfb_erweima['codeurl'][0]['#text'];//返回的二维码
			$wxarr['outtradeno'] = $xfb_erweima['outtradeno'][0]['#text'];//订单唯一标示
			$wxarr['shorturl'] = $xfb_erweima['shorturl'][0]['#text'];//二维码地址
			return $wxarr;
		}*/
    }
	//支付宝
	public  function zfb()
	{
		//支付宝
		$zarr['orderid']=$this->pay_sn;//订单号
		$zarr['merid']=$this->shang_hu_hao;//商户号
		$zarr['totalfee']=$this->pay_amount_online;//消费总金额
		$zarr['body']='祥付宝支付';//交易金额
		$zarr['subject']='订单标题';//订单标题
		$zarr['paymethod']='zfb';//支付方式
		$zarr['funname']='prepay';//
		$zarr['payway']='scancode';
		$zarr['zfbtwo']='zfbtwo';
		//$zarr['ordertime']=5;//超时时间
		//$xfbarr['tradetype']='JSAPI';//微信内线上支付 否则不用
		$xfb_erweima = $this->xfbpay->send_xfb($zarr,$this->mishi,1);
		return $xfb_erweima;
		/*$flag = $xfb_erweima['flag'][0]['#text'];
		//获取二维码成功
		if($flag==00){
			$wxarr['codeurl'] = $xfb_erweima['codeurl'][0]['#text'];//返回的二维码
			$wxarr['outtradeno'] = $xfb_erweima['outtradeno'][0]['#text'];//订单唯一标示
			$wxarr['shorturl'] = $xfb_erweima['shorturl'][0]['#text'];//二维码地址
			return $wxarr; 
			//Tpl::output('zcodeurl',$codeurl);
		}else{
			var_dump($xfb_erweima);
		}*/
	}
	//条码支付查询
	public function select_order($paymethod)
	{
		//订单查询接口
		$arr['merid']=$this->shang_hu_hao;
		$arr['orderid']=$this->pay_sn;
		$arr['paymethod']=$paymethod;
		$arr['funname']='orderquery';
		if($arr['paymethod']=='zfb')
		{
			$arr['zfbtwo']='zfbtwo';
		}
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,1);
		var_dump($xfb_erweima);

	}
	//扫码支付查询
	public function barcode_pay($paymethod)
	{
		//订单查询接口
		$arr['merid']=$this->shang_hu_hao;
		$arr['orderid']=$this->pay_sn;
		$arr['paymethod']=$paymethod;
		$arr['funname']='orderquery';
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,1);
		var_dump($xfb_erweima);
	}
	//扫码支付
	public function scan_code_pay()
	{
		$arr['weixin']=$this->weixin();//微信支付
		//$arr['zfb']=$this->zfb();//支付宝支付
		var_dump($arr);
	}
	//撤销订单
	public function cancel_order($paymethod)
	{
		$arr['merid']=$this->shang_hu_hao;
		$arr['orderid']=$this->pay_sn;
		$arr['paymethod']=$paymethod;
		$arr['funname']='closeorder';
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,1);
		var_dump($xfb_erweima);
	}
	//退款
	public function refund($paymethod)
	{
		$arr['merid']=$this->shang_hu_hao;
		$arr['orderid']=$this->pay_sn;
		$arr['paymethod']=$paymethod;
		$arr['refundamount']=0.01;
		$arr['funname']='refund';
		if($arr['paymethod']=='zfb')
		{
			$arr['zfbtwo']='zfbtwo';
		}
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,1);
		var_dump($xfb_erweima);
	}
	//统一订单查询
	public function uniform_order_query()
	{
		$arr['merid']=$this->shang_hu_hao;
		$arr['orderid']=$this->pay_sn;
		$arr['paymethod']='all';
		$arr['funname']='queryall';
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,1);
		var_dump($xfb_erweima);
	}
	//对账单下载接口
	public function account_statement()
	{
		$arr['merid']=$this->shang_hu_hao;
		$arr['billdate']='20161229';
		$arr['paytype']=0;
		$arr['datatype']='ALL';
		$xfb_erweima = $this->xfbpay->send_xfb($arr,$this->mishi,2);//为2  对账单接口地址
		var_dump($xfb_erweima);
	}
	//异步通知
	 public function asynchronous_notific()
	 {
		$get_sign = $_GET['sign'];
		unset($_GET['sign']);
		//$sign='F77EB4866C82416E78478DC40E2F74EF';
		/*$arr['orderid'] = $_GET['orderid'];//商户订单号
		$arr['outtranno'] = $_GET['outtranno'];//祥付宝订单号
		$arr['tradestate'] = $_GET['tradestate'];//交易状态(TRADE_SUCCESS交易成功,TRADE_REFUND,退款，REVOKED 撤销)
		$arr['orderamt'] = $_GET['orderamt'];//交易金额
		//$arr['storecode'] = $_GET['storecode'];//店面号
		$arr['method'] = $_GET['method'];//支付方式，说明见1.14
		$arr['tradetime'] = $_GET['tradetime'];//交易时间 格式yyyyMMddhhmmss
		$arr['thirdorderid'] = $_GET['thirdorderid'];//微信/支付宝等第三方交易号	
		//$arr['oldorder'] = $_GET['oldorder'];原单号（退款会返回）*/
		foreach($_GET as $key=>$value)
		{
			$arrStr[]=$key.'='.$value;
		}
		$sign = $this->xfbpay($arrStr,$this->mishi);//生成sign
		if($get_sign==$sign)
		{
			if($_GET['tradestate']=='TRADE_SUCCESS')
			{
				echo 'success';//成功
			}
		}
	 }
	
}
$demo = new demo();
//$demo->jsapi_pre_order();//公众号微信h5支付
$demo->scan_code_pay();//扫码支付(预下单)
//$demo->select_order($paymethod='wx');//条码支付订单查询
//$demo->barcode_pay($paymethod='wx');//扫码支付订单查询
//$demo->cancel_order($paymethod='wx');//撤销订单
//$demo->refund($paymethod='wx');//撤销订单
//$demo->account_statement();//统一订单查询
//$demo->asynchronous_notific();//异步通知
?>